<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form
                class="mb-[-16px]"
                :model="queryParams"
                inline
            >
                <el-form-item label="名称" prop="title">
                    <el-input class="w-[280px]" v-model="queryParams.title" clearable placeholder="请输入名称" />
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <!-- <el-button v-perms="['hotel_basic/add']" type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                新增
            </el-button> -->
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <!-- <el-table-column label="名称" prop="title" show-overflow-tooltip /> -->
                    <el-table-column label="视频" show-overflow-tooltip >
                        <template #default="{ row }">
                            <div class="video-placeholder" @click="openVideoDialog(row)">
                                <div class="play-icon-large">
                                    <el-icon><VideoPlay /></el-icon>
                                    <!-- <span>{{ scope.row.name }}</span> -->
                                </div>
                            </div>
                            <!-- <video ref="videoRef" controls muted autoplay style="width: 300px;height: 300px;">
                                <source :src="row.video" type="video/mp4">
                                您的浏览器不支持HTML5视频播放。
                            </video> -->
                            <!-- <video :src="row.video" style="width: 300px;height: 300px;"></video> -->
                        </template>
                    </el-table-column>

                    <el-table-column label="操作" width="180" fixed="right" align="center">
                        <template #default="{ row }">
                            <el-button
                                v-perms="['hotel_basic/edit']"
                                type="primary"
                                link
                                @click="handleEdit(row)"
                            >
                                编辑
                            </el-button>
                            <el-button v-if="row.status == 1"
                                v-perms="['hotel_basic/delete']"
                                type="danger"
                                link
                                @click="handleDelete(row.id)"
                            >
                                删除
                            </el-button>
                            <el-button v-if="row.status == 0"
                                v-perms="['hotel_basic/recover']"
                                type="primary"
                                link
                                @click="handleRecover(row.id)"
                            >
                                恢复
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <el-dialog v-model="dialogVisible" title="视频播放" width="80%">
                <div class="dialog-video-container">
                    <video ref="videoRef" autoplay controls>
                        <source :src="currentVideo" type="video/mp4">
                        您的浏览器不支持视频播放
                    </video>
                </div>
            <template #footer>
                <el-button @click="dialogVisible = false">关闭</el-button>
            </template>
        </el-dialog>
        <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup name="hotelBasicLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiMuVideoLists, apiMuVideoDelete, apiMuVideoRecover } from '@/api/graveyardManage/video'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edits.vue'
import { reactive, ref, shallowRef, computed, nextTick } from 'vue'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)


// 查询条件
const queryParams = reactive({
    title: '',
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 获取字典数据
const { dictData } = useDictData('')

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiMuVideoLists,
    params: queryParams
})

// 添加
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

// 删除
const handleDelete = async (id: number | any[]) => {
    await feedback.confirm('确定要删除？')
    await apiMuVideoDelete({ id })
    getLists()
}

// 恢复
const handleRecover = async (id: number | any[]) => {
    await feedback.confirm('确定要恢复？')
    await apiMuVideoRecover({ id })
    getLists()
}




// ======================================================================================
const dialogVisible = ref(false);
const currentVideo = ref('');
const videoRef = ref<HTMLVideoElement | null>(null);

const openVideoDialog = (row: any) => {
    console.log(row)
  currentVideo.value = row.video;
  dialogVisible.value = true;
};

const closeVideoDialog = () => {
  dialogVisible.value = false;
  videoRef.value?.pause();
};

watch(dialogVisible, (val) => {
  if (val && videoRef.value) {
    nextTick(() => {
      videoRef.value?.play().catch(err => {
        console.error('视频播放失败:', err);
      });
    });
  }
});

onMounted(() => {
  // 预加载视频元数据
//   pager.value.forEach(video => {
//     const videoElement = document.createElement('video');
//     videoElement.src = video.url;
//     videoElement.preload = 'metadata';
//   });
});
// ======================================================================================













getLists()
</script>

<style scoped>
    .video-placeholder {
  position: relative;
  width: 200px;
  height: 112px;
  cursor: pointer;
  border-radius: 4px;
  background-color: #f5f7fa;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #ebeef5;
  transition: background-color 0.3s;
}

.video-placeholder:hover {
  background-color: #e4e7ed;
}

.play-icon-large {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: #909399;
}

.play-icon-large i {
  font-size: 40px;
  margin-bottom: 8px;
  transition: transform 0.3s;
}

.video-placeholder:hover .play-icon-large i {
  transform: scale(1.1);
  color: #409eff;
}


.dialog-video-container {
  width: 100%;
  height: 0;
  padding-bottom: 40.25%; /* 16:9 比例 */
  position: relative;
}

.dialog-video-container video {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  border-radius: 4px;
}
</style>